package edu.tsinghua.oxy.common.repository.impl;

import edu.tsinghua.oxy.common.repository.IBaseRedisRepository;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

/**
 * Redis的操作接口
 *
 * @author sean
 * @date 2021 - 07 - 29 上午10:54
 */
@Repository
public class RedisRepositoryImpl implements IBaseRedisRepository {

    @Resource
    private StringRedisTemplate template;


    @Override
    public void setKey(String key, String value) {
        template.opsForValue().set(key, value);
    }

    @Override
    public String getValue(String key) {
        return template.opsForValue().get(key);
    }

    @Override
    public void sadd(String key, String ... value) {
        template.opsForSet().add(key,value);
    }

    @Override
    public void sadd(String key, Collection<String> value) {
        template.opsForSet().add(key,value.toArray(new String[0]));
    }

    @Override
    public String spop(String key) {
        return template.opsForSet().pop(key);
    }

    @Override
    public String srandmember(String key) {
        return template.opsForSet().randomMember(key);
    }

    @Override
    public Set<String> smembers(String key) {
        return template.opsForSet().members(key);
    }

    @Override
    public void del(String ... key) {
        template.delete(Arrays.asList(key));
    }

    @Override
    public Long incr(String key) {
        return template.opsForValue().increment(key);
    }
}

